import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage, ElMessageBox} from "element-plus";

const routes = [
    {
        path: "/login",
        name: "name",
        component: () => import("../views/Login.vue")
    },
    {
        path: "/",
        name: "Home",
        redirect:"/userCenter",
        component: ()=>import("../views/Home.vue"),

        children:[
            {
                path:'/square',
                name:'Square',
                component: ()=>import("../views/HomePage/Square.vue")
            },
            {
                path:'/friend',
                name:'Friend',
                component: ()=>import("../views/HomePage/Friend.vue")
            },
            {
                path:'/message',
                name:'Message',
                component:()=>import("../views/HomePage/Message.vue")
            },
            {
                path:'/userCenter',
                name:'UserCenter',
                component:()=>import("../views/HomePage/UserCenter.vue")
            },
            {
                path:'/resetPassword',
                name:'ResetPassword',
                component:()=>import("../views/HomePage/ResetPassword.vue")
            }
        ]
    }
]





const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


// 路由守卫
router.beforeEach((to, form, next)=>{
    let JWT = localStorage.getItem("jwt");
    if(!JWT && to.path !== '/login'){
        ElMessage({
            message: '请先注册登录',
            type: 'info',
        })
        // 没有token且跳转目的地址不是登录界面 就强制跳转到登录页面
        return next('/login')
    }
//    有JWT或者目的地址是登录页
    next()
})


export default router
